package com.project.movie.controller.item;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.movie.common.enums.BusinessType;
import com.project.movie.common.enums.ResultCode;
import com.project.movie.domain.*;
import com.project.movie.service.MovieCommentService;
import com.project.movie.service.MovieItemService;
import com.project.movie.service.MoviePerformerService;
import com.project.movie.service.MovieSliceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 电影controller
 * @date 2025/02/06 09:52
 */
@Controller
@ResponseBody
@RequestMapping("item")
public class MovieItemController {

    @Autowired
    private MovieItemService movieItemService;
    @Autowired
    private MoviePerformerService moviePerformerService;
    @Autowired
    private MovieCommentService movieCommentService;
    @Autowired
    private MovieSliceService movieSliceService;

    /** 分页获取电影 */
    @PostMapping("getMovieItemPage")
    public Result getMovieItemPage(@RequestBody MovieItem movieItem) {
        Page<MovieItem> page = new Page<>(movieItem.getPageNumber(),movieItem.getPageSize());
        QueryWrapper<MovieItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.isNotBlank(movieItem.getName()),MovieItem::getName,movieItem.getName())
                .like(StringUtils.isNotBlank(movieItem.getDirector()),MovieItem::getDirector,movieItem.getDirector())
                .like(StringUtils.isNotBlank(movieItem.getType()),MovieItem::getType,movieItem.getType())
                .like(StringUtils.isNotBlank(movieItem.getRegion()),MovieItem::getRegion,movieItem.getRegion())
                .like(StringUtils.isNotBlank(movieItem.getLanguage()),MovieItem::getLanguage,movieItem.getLanguage())
                .eq(movieItem.getState() != null,MovieItem::getState,movieItem.getState())
                .orderByDesc(MovieItem::getCreateTime);
        Page<MovieItem> movieItemPage = movieItemService.page(page, queryWrapper);
        return Result.success(movieItemPage);
    }

    @GetMapping("getMovieItemList")
    public Result getMovieItemList() {
        List<MovieItem> list = movieItemService.list();
        return Result.success(list);
    }

    @GetMapping("getMovieItemRank")
    public Result getMovieItemRank() {
        JSONObject jsonObject = new JSONObject();
        List<MovieItem> money = movieItemService.getMovieMoneyRank();
        jsonObject.put("money",money);
        QueryWrapper<MovieItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(MovieItem::getScore).last("limit 20");
        List<MovieItem> score = movieItemService.list(queryWrapper);
        jsonObject.put("score",score);
        QueryWrapper<MovieItem> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByDesc(MovieItem::getLikeNum).last("limit 20");
        List<MovieItem> likeNum = movieItemService.list(wrapper);
        jsonObject.put("likeNum",likeNum);
        return Result.success(jsonObject);
    }

    @GetMapping("getRecommandMovie")
    public Result getRecommandMovie() {
        QueryWrapper<MovieItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MovieItem::getRecommand,0);
        List<MovieItem> movieItemList = movieItemService.list(queryWrapper);
        return Result.success(movieItemList);
    }

    @GetMapping("getMovieItemIndex")
    public Result getMovieItemIndex(@RequestParam("state")Integer state) {
        QueryWrapper<MovieItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MovieItem::getState,state).last("limit 6");
        List<MovieItem> itemList = movieItemService.list(queryWrapper);
        return Result.success(itemList);
    }

    /** 根据id获取电影 */
    @GetMapping("getMovieItemById")
    public Result getMovieItemById(@RequestParam("id")String id) {
        MovieItem movieItem = movieItemService.getById(id);
        QueryWrapper<MoviePerformer> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MoviePerformer::getItemId,id);
        List<MoviePerformer> performerList = moviePerformerService.list(queryWrapper);
        List<String> performer = new ArrayList<>();
        for (MoviePerformer moviePerformer : performerList) {
            performer.add(moviePerformer.getName());
        }
        movieItem.setPerformer(StringUtils.join(performer,","));
        return Result.success(movieItem);
    }

    /** 保存电影 */
    @PostMapping("saveMovieItem")
    public Result saveMovieItem(@RequestBody MovieItem movieItem) {
        boolean save = movieItemService.save(movieItem);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑电影 */
    @PostMapping("editMovieItem")
    public Result editMovieItem(@RequestBody MovieItem movieItem) {
        boolean save = movieItemService.updateById(movieItem);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除电影 */
    @GetMapping("removeMovieItem")
    @Transactional(rollbackFor = Exception.class)
    public Result removeMovieItem(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                //先判断有没有排片
                QueryWrapper<MovieSlice> query = new QueryWrapper<>();
                query.lambda().eq(MovieSlice::getItemId,id);
                int count = movieSliceService.count(query);
                if (count > 0) {
                    return Result.fail("存在排片无法删除");
                }
                //删除演员
                QueryWrapper<MoviePerformer>  queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(MoviePerformer::getItemId,id);
                moviePerformerService.remove(queryWrapper);
                //删除评论
                QueryWrapper<MovieComment> wrapper = new QueryWrapper<>();
                wrapper.lambda().eq(MovieComment::getItemId,id);
                movieCommentService.remove(wrapper);
                movieItemService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("电影id不能为空！");
        }
    }

}
